package com.sunday.android.fangdaifu.bukong.fragment.index;


import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.adapter.EquipmentWithHeadersAdapter;
import com.sunday.android.fangdaifu.bukong.adapter.IndexWithHeadersAdapter;
import com.sunday.android.fangdaifu.bukong.entity.Equipments;
import com.sunday.android.fangdaifu.bukong.entity.IndexItem;
import com.sunday.android.fangdaifu.bukong.entity.InstallEquipment;
import com.sunday.android.fangdaifu.bukong.entity.event.EquipmentList;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.AminUtils;
import com.sunday.android.fangdaifu.bukong.utils.LazyFragment;
import com.sunday.android.fangdaifu.bukong.utils.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.common.widgets.recyclerView.VerticalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * 设备匹配
 */
public class EquipmentFragment extends LazyFragment {


    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @Bind(R.id.emptylayout)
    EmptyLayout emptylayout;

    private long id;
    private int type;
    private List<InstallEquipment> installEquipmentList = new ArrayList<>();
    private EquipmentWithHeadersAdapter adapter;
    private StickyRecyclerHeadersDecoration headersDecor;
    private boolean isPrepared;
    private boolean mHasLoadedOnce;

    public EquipmentFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_equipment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        id = bundle.getLong("id");
        type = bundle.getInt("type");
        initView();
        isPrepared = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        lazyLoad();
    }

    //初始化布局
    private void initView() {
        refreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        //设置列表数据和浮动header
        final LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new EquipmentWithHeadersAdapter(mContext,installEquipmentList);
        recyclerView.setAdapter(adapter);
        adapter.setOpen(new EquipmentWithHeadersAdapter.OnOpenListener() {
            @Override
            public void toggle(int position,boolean isOpen,View more) {
                installEquipmentList.get(position).setHaveOpen(isOpen);
                adapter.notifyItemChanged(position);
            }
        });
    }

    //可见后加载数据
    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        loading();
        getData();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loading();
                getData();
            }
        });
    }

    private void getData() {
        Call<ResultDO<List<InstallEquipment>>> call = ApiClient.getApiAdapter().AePointList(id,type);
        call.enqueue(new Callback<ResultDO<List<InstallEquipment>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<InstallEquipment>>> call, Response<ResultDO<List<InstallEquipment>>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                refreshLayout.setRefreshing(false);
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    installEquipmentList.clear();
                    installEquipmentList.addAll(response.body().getResult());
                    for (InstallEquipment em :installEquipmentList ){
                        em.setHaveOpen(true);
                    }
                    EquipmentList equipmentL = new EquipmentList();
                    equipmentL.size = installEquipmentList.size();
                    equipmentL.type = type;
                    EventBus.getDefault().post(equipmentL);
                    if (installEquipmentList.isEmpty()){
                        emptylayout.setErrorType(EmptyLayout.NODATA);
                        emptylayout.setErrorMessage("暂无数据");
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<InstallEquipment>>> call, Throwable t) {
                if (isFinish) {
                    return;
                }
                refreshLayout.setRefreshing(false);
            }
        });
    }

    private void loading(){
        if (!refreshLayout.isRefreshing()){
            refreshLayout.setRefreshing(true);
        }
    }

    public static EquipmentFragment instance(long i,int type){
        EquipmentFragment fragment = new EquipmentFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("id",i);
        bundle.putInt("type",type);
        fragment.setArguments(bundle);
        return fragment;
    }

}
